***		Title: 		American Local Leaders Survey (ALLS) 
***		Paper: 		Election Confidence
***		Authors: 	Gary Adler & Eric Plutzer
***		Updated:	10/04/2024

clear all
capture log close
set more off

cd "ENTER FULL PATH NAME OF DATA FOLDER HERE"

global GARY "C:\Users\gja13\The Pennsylvania State University\Church and State Lab - Documents\NSF Project\Papers\Confidence in Elections\PLoS submission\"
cd "$GARY"

**
global ERIC "C:\Users\exp12\OneDrive - The Pennsylvania State University\NSF Project\Papers\Confidence in Elections\PLoS submission\"
cd "$ERIC"


*******************************************************************************************

* Read in survey data
use "replication_data_set.dta"

* Classify data as survey data to be analyzed with inverse probability weight
svyset [pw=anal_weight]

* Integer weight to use in histograms
gen integer_weight = round(100*anal_weight)

* Flag for respondents who answered one of the confidence questions
gen conf_flag = 0
replace conf_flag = 1 if vote_conf_local~=. | vote_conf_nation~=.

* Flag to distinguish county and municipal officials
label variable county_sample "County (vs municipal) official"

***************************************************
* Flag source as original survey data
gen source=1

***************************************************************
* COMPARE TO MIT 2022 SURVEY
*      Subset includes following variables:
*    	anal_weight pid3 pid7 source st_fip
*		vote_conf_yourvote vote_conf_local vote_conf_state vote_conf_nation 

append using "MIT2022_subset.dta", generate(append)

label define sour 1 "Local officials"
label define sour 2 "MIT 2022", add
label values source sour



** Table 1
svy: tab vote_conf_local source if elected==1 | source==2, column percent format(%20.1f) pearson stubwidth(25)
svy: tab vote_conf_nation source if elected==1 | source==2, column percent format(%20.1f) pearson stubwidth(25)

* Runs to assess statistical signifcance of specific comparisons
* Local confidence - main effect of source (OR =  .314, t = - 10.34, p < .000)
svy linearized : ologit vote_conf_local source if elected==1 | source==2, or
* Local confidence - main effect of source with state fixed effects (OR =  .322, t = - 9.99, p < .000)
svy linearized : ologit vote_conf_local source ib6.st_fip if elected==1 | source==2, or

* National confidence - main effect of source (OR =  .597, t = - 6.10, p < .000)
svy linearized : ologit vote_conf_nation source if elected==1 | source==2, or
* National confidence - main effect of source with state fixed effects (OR =  .614, t = - 5.72, p < .000)
svy linearized : ologit vote_conf_nation source ib6.st_fip if elected==1 | source==2, or

* Drop MIT data
drop if source==2



*********** DEPENDENT VARIABLES ***********
fre vote_conf_local vote_conf_nation


******************************************* 
* Independent variables

**** Context measures
* Population size - quintiles 
xtile pop_quintile_cty = acs_totpop if county_sample==1, nq(5)
xtile pop_quintile_cit = acs_totpop if county_sample==0, nq(5)
egen pop_quintile = rowmax(pop_quintile_cty pop_quintile_cit)
drop pop_quintile_cty pop_quintile_cit
label variable pop_quintile "Community size (1-5)"

* Trump share of the county presidential vote
* Convert 0-100 to 0-10 for ease of interpretation
gen rep_2020_10pt = rep_2020_share/10
label variable rep_2020_10pt "2020 Trump vote (0-10)"

**** Survey measures


label variable partyid "Party ID (0-6)"


** Standard demographics

/* Sex (note only 9 individuals selected something else other than man or woman)
*  Their answers to lgbtorno: 
	2 ID as lesbian or gay, 
	2 as heterosexual, 
	1 as bisexual, 
	4 as prefer not to say or missing 
*/

gen female = 0
replace female = 1 if sex_gend==2
label variable female "Female"

* Sexual orientation (95% ID as straight)
gen lgbqt=0
replace lgbqt = 1 if lgbtorno == 1 // Lesbian or gay (3%)
replace lgbqt = 1 if lgbtorno == 3 // Bisexual (1.6%)
replace lgbqt = 0 if lgbtorno == 4 // Prefer not to say - could also declare as missing (3%)
replace lgbqt = 1 if sex_gend == 3 & lgbtorno ~= 2 // Something else & not heterosexual (.4%)
label variable lgbqt "Identifies as LGBQ or T"

	* Fixing respondents with troll answers:
	replace lgbqt = 0 if idcode5 == 16410  // "Apache Attack Helicopter"
	replace lgbqt = 0 if idcode5 == 22960  // "What else is there?"
	replace lgbqt = 0 if idcode5 == 11752  // "Either your a man or woman."
	replace lgbqt = 0 if idcode5 == 16111   // "Relevancy?" 


* Marital status (from 7 categories to 5)
gen marital_summary = .
replace marital_summary = 1 if mar_stat==3 // Married/Civil union (78% of sample)
replace marital_summary = 1 if mar_stat==2 // Living with partner ( 3%)
replace marital_summary = 2 if mar_stat==1 // Divorced  ( 7.5%)
replace marital_summary = 2 if mar_stat==4 // Separated ( 0.5%)
replace marital_summary = 3 if mar_stat==6 // Widowed   ( 2.6%)
replace marital_summary = 4 if mar_stat==5 // Never married   ( 4%)
replace marital_summary = 5 if mar_stat==7 // Refused   (1.5%)

gen married=0
replace married=1 if mar_stat==3
label variable married "Married or civil union"


label define mart 1 "Married or union"
label define mart 2 "Divorced or separated", add
label define mart 3 "Widowed", add
label define mart 4 "Never married", add
label define mart 5 "Declined", add
label values marital_summary mart
label variable marital_summary "Marital status"

clonevar child18=childnum
replace child18=3 if childnum >3 & childnum<99 // Topcoding 2% of sample with 4-10 children to 3 or more
label define c18 0 "None"
label define c18 1 "One", add
label define c18 2 "Two", add
label define c18 3 "Three or more", add
label values child18 c18

gen mil_household=0
replace mil_household=1 if military<=4 
label variable mil_household "Military household"

sum age 
label variable age "Age in years"
fre degree
label variable degree "Education (1-7)"

gen coll_degree=0
replace coll_degree=1 if degree==5
label variable coll_degree "Bachelor degree"
gen grad_degree=0
replace grad_degree=1 if degree==6 | degree==7
label variable grad_degree "Graduate degree"

gen educ=0
replace educ=1 if coll_degree==1
replace educ=2 if grad_degree==1
replace educ=. if degree==.

label define educlabel 0 "Less than Bachelor"
label define educlabel 1 "College degree", add
label define educlabel 2 "Graduate degree", add
label values educ educlabel
label variable educ "Education"


*Shorter reltrad combines non-Christians
clonevar reltrad_short = reltrad
replace reltrad_short = 6 if reltrad_short==5



* SOCIAL TRUST VARIABLES

/** Social trust (1-4 with 4 = completely)
tr_famil        Trusts family
tr_nborh        Trusts people in neighborhood
tr_nwppl        Trusts people met for the first time
tr_dfrel        Trusts people from a different religion
tr_dfpol        Trusts people from a different political party
*/

fre tr_famil tr_nborh tr_nwppl tr_dfrel tr_dfpol  [aw=anal_weight]
sum tr_famil tr_nborh tr_nwppl tr_dfrel tr_dfpol 

* Single scale
factor tr_famil tr_nborh tr_nwppl tr_dfrel tr_dfpol [aw=anal_weight] , pcf // Eig 1.03 for F2
predict trust_factor_score
label variable trust_factor_score "Social trust (z-score)"
sum trust_factor_score [aw=anal_weight], detail

alpha tr_famil tr_nborh tr_nwppl tr_dfrel tr_dfpol, std item detail // alpha = 0.59
* Figure 1
histogram trust_factor_score [fw=integer_weight], bin(15) scheme(plotplain) percent


* Party id and ideology
gen folded_pid = abs(partyid-3)
recode polviews (8=3) // Coding to moderate


** Role-related characteristics
label variable elected "Elected official"

gen seniority=.
replace seniority = 1 if gov_years_numeric < 2
replace seniority = 2 if gov_years_numeric >= 2 & gov_years_numeric<5
replace seniority = 3 if gov_years_numeric >=5 & gov_years_numeric <10
replace seniority = 4 if gov_years_numeric >=10 & gov_years_numeric <.

label define sen 1 "Under 2 yrs"
label define sen 2 "2-5 yrs", add
label define sen 3 "5-10 yrs", add
label define sen 4 "10 or more yrs", add
label values seniority sen
label variable seniority "Years in current position"
fre seniority


* Ambition
gen ambition=0
replace ambition=1 if futurhir==2 | futurhir==3
replace ambition=1 if futurelc==2 | futurelc==3
label variable ambition "Open to higher office in future"


****************** TABLE G1:SUMMARY STATISTICS ******************
* Panel A: Continuous measures
univar partyid rep_2020_10pt trust_factor_score   age religiosity [aw=anal_weight]

* Panel B: Binary measures
univar responsible_jurisdiction ambition  hh_military  hispanic married female lgbqt  black [aw=anal_weight]

* Panel C: Ordinal and categorical variables
fre pop_quintile educ seniority child18 reltrad [aw=anal_weight]


****************************************************************
** CONFIDENCE MODELS FOR PAPER


******************************

* CONTROLS ONLY
* Local confidence
svy: ologit vote_conf_local  /// 
pop_quintile seniority /// 
hh_military i.educ hispanic married i.child18 female lgbqt age b1.reltrad religiosity 
estimates store loc0, title(Controls only)

* National confidence
svy: ologit vote_conf_nation vote_conf_local  /// 
pop_quintile seniority /// 
hh_military i.educ hispanic married i.child18 female lgbqt age b1.reltrad religiosity
estimates store nat0, title(Controls only)


* HYPOTHESIZED MAIN EFFECTS (Responsible, PID and Partisan Context, and Ambition)
* Local confidence
svy: ologit vote_conf_local  /// 
responsible_jurisdiction  partyid rep_2020_10pt trust_factor_score ambition ///
denial_score ///
pop_quintile seniority /// 
hh_military i.educ hispanic married i.child18 female lgbqt age b1.reltrad religiosity 
estimates store loc1, title(With key IVs) 
margins, at(responsible_jurisdiction=(0/1)) predict(outcome(4)) atmeans
margins, at(partyid=(0/6)) predict(outcome(4)) atmeans
margins, at(trust_factor_score=(-2/2)) predict(outcome(4)) atmeans

* National confidence
svy: ologit vote_conf_nation vote_conf_local  /// 
responsible_jurisdiction partyid rep_2020_10pt trust_factor_score ambition ///
denial_score ///
pop_quintile seniority /// 
hh_military i.educ hispanic married i.child18 female lgbqt age b1.reltrad religiosity
estimates store nat1, title(With key IVs) 
margins, at(responsible_jurisdiction=(0/1)) predict(outcome(4)) atmeans
margins, at(partyid=(0/6)) predict(outcome(4)) atmeans
margins, at(trust_factor_score=(-2/2)) predict(outcome(4)) atmeans


* Figure 2
coefplot (loc1, label(Local) mcolor(black) msymbol(D) msize(medlarge)) ///
	(nat1, label(National) mcolor(sienna) msymbol(O) msize(medlarge)) ///
	, keep(responsible_jurisdiction partyid rep_2020_10pt trust_factor_score  ) /// 
	base grid(none) ylabel(, noticks) ///
	levels(95)  scheme(plotplain) xline(0, lcolor(red%40)) ///
	xlabel(-1.0(.25)1.0) xtitle("   " "  Logit Estimate  " ///
				 "More skeptical           More confident" )  ///
	name(natlocdot, replace)

* Appendix table supporting Figure 2, including controls
etable , estimates(loc1 nat1) column(index) ///
	cstat(_r_b, nformat(%9.3f)) 	///
	cstat(_r_se, nformat(%9.3f))	///
	showstars showstarsnote ///
	mstat(N,   nformat(%8.0fc) label("Observations"))	///
	mstat(r2, nformat(%9.4f) label("R2"))	///
	title(Table G2: Logit Models Predicting Confidence in Local and National Elections) ///
	export(Table_G2_logit.xlsx, replace)

************ GOP ONLY MODELS ***********

* Local confidence
svy: ologit vote_conf_local  /// 
responsible_jurisdiction partyid rep_2020_10pt trust_factor_score denial_score ambition ///
pop_quintile seniority /// 
hh_military i.educ hispanic married i.child18 female lgbqt age b1.reltrad religiosity if partyid>=4
estimates store loc2gop, title(GOP leadership cues)
margins  if partyid>=4, at(ambition=(0/1)) predict(outcome(4)) atmeans


* National confidence
svy: ologit vote_conf_nation vote_conf_local  ///
responsible_jurisdiction partyid rep_2020_10pt trust_factor_score denial_score ambition ///
pop_quintile seniority /// 
hh_military i.educ hispanic married i.child18 female lgbqt age b1.reltrad religiosity if partyid>=4 
estimates store nat2gop, title(GOP leadership cues)
margins  if partyid>=4, at(ambition=(0/1)) predict(outcome(4)) atmeans

* Figure 2
coefplot (loc2gop, label(Local) mcolor(black) msymbol(D) msize(medlarge)) ///
	(nat2gop, label(National) mcolor(sienna) msymbol(O) msize(medlarge)) ///
	, keep(responsible_jurisdiction partyid rep_2020_10pt trust_factor_score denial_score ambition) /// 
	base grid(none) ylabel(, noticks) ///
	levels(95)  scheme(plotplain) xline(0, lcolor(red%40)) ///
	xlabel(-1.5(.5)1.5) xtitle("   " "  Logit Estimate  " ///
				 "More skeptical           More confident" )  ///
	name(gopdot, replace)
* Appendix table supporting Figure 3, including controls
etable , estimates(loc2gop nat2gop) column(index) ///
	cstat(_r_b, nformat(%9.3f)) 	///
	cstat(_r_se, nformat(%9.3f))	///
	showstars showstarsnote ///
	mstat(N,   nformat(%8.0fc) label("Observations"))	///
	mstat(r2, nformat(%9.4f) label("R2"))	///
	title(Table G3: Logit Models Predicting Confidence in Local and National Elections) ///
	export(Table_G3_logit.xlsx, replace)


* SAME, BUT WITH AMBITION INTERACTION
* Interactions are never close to significant.
* Local confidence (interaction ns, t = -0.86, p = 0.39)
svy: ologit vote_conf_local  /// 
responsible_jurisdiction partyid rep_2020_10pt trust_factor_score i.ambition##c.denial_score ///
pop_quintile seniority /// 
hh_military i.educ hispanic married i.child18 female lgbqt age b1.reltrad religiosity if partyid>=4
estimates store loc2gop_i, title(GOP leadership cues)

* National confidence (interaction ns, t = 1.47, p = 0.14)
svy: ologit vote_conf_nation vote_conf_local  ///
responsible_jurisdiction partyid rep_2020_10pt trust_factor_score i.ambition##c.denial_score ///
denial_score ///
pop_quintile seniority /// 
hh_military i.educ hispanic married i.child18 female lgbqt age b1.reltrad religiosity if partyid>=4
estimates store nat2gop_i, title(GOP leadership cues)

** Correlation reported in the discussion
corr partyid trust_factor_score [aw=anal_weight]


